﻿using System.Drawing;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
using System.Windows;

namespace CommonUtils
{
    /// <summary>
    /// 系统工具
    /// </summary>
    public static class SystemUtil
    {
        /// <summary>
        /// 主机名
        /// </summary>
        public static string HostName { private set; get; }

        /// <summary>
        /// 主机名
        /// </summary>
        public static string HostNameAndTime
        {
            get
            {
                return string.Format("{0}[{1}]", HostName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            }
        }

        /// <summary>
        /// 主机类
        /// </summary>
        public static IPHostEntry HostEntry { private set; get; }

        /// <summary>
        /// ip集
        /// </summary>
        public static IPAddress[] HostIps { private set; get; }

        /// <summary>
        /// ip
        /// </summary>
        public static IPAddress HostIp { private set; get; }

        static SystemUtil()
        {
            HostName = Dns.GetHostName();
            HostEntry = Dns.GetHostEntry(HostName);
            HostIps = Dns.GetHostAddresses(HostName);

            if (HostIps.Length > 0)
                HostIp = HostIps[0];
        }

        /// <summary>
        /// Hosts路径
        /// </summary>
        public const string DnsPath = @"C:\Windows\System32\drivers\etc\hosts";

        /// <summary>
        /// 设置解析
        /// </summary>
        public static void SetDns(string domainName, string ip)
        {
            string newHost = ip + " " + domainName;
            string[] hosts = CommonUtils.FileUtil.GetLines(DnsPath);
            foreach (string host in hosts)
                if (host == newHost)
                    return;
            CommonUtils.FileUtil.AppendLine(DnsPath, newHost);
        }

        /// <summary>
        /// 删除解析
        /// </summary>
        public static void RemoveDns(string domainName, string ip)
        {
            string delHost = ip + " " + domainName;
            string[] hosts = CommonUtils.FileUtil.GetLines(DnsPath);
            List<string> nesHosts = new List<string>();
            bool needReplace = false;
            foreach (string host in hosts)
            {
                if (host != delHost)
                    nesHosts.Add(host);
                else
                    needReplace = true;
            }
            if (needReplace)
                CommonUtils.FileUtil.Save(DnsPath, nesHosts);
        }

        /// <summary>
        /// ping通返回true,否则返回false
        /// </summary>
        public static bool Ping(string ipOrUrl)
        {
            int timeOut = 3000;
            try
            {
                Ping ping = new Ping();
                PingReply reply = ping.Send(ipOrUrl, timeOut);
                bool result = reply.Status == IPStatus.Success;
                ping.Dispose();
                return result;
            }
            catch
            {
                return false;
            }
        }

        private static string _clipboardPath = PathConfig.GetTemp("clipboard.txt");
        private static string _clipboardPathWithQuotation = _clipboardPath.AddDoubleQuotes();

        public static void SetClipboard(string text)
        {
            FileUtil.Save(_clipboardPath, text, TextEncoding.UTF8NoBom);
            //CommandUtil.Run(string.Format("echo {0}|clip", text));
            CommandUtil.Run(string.Format("clip < {0}", _clipboardPathWithQuotation));
        }

        public static void SetToClipboard(this string text)
        => SetClipboard(text);

        public static readonly string DownloadsFolder = SystemArguments.UserDownloadsFolder;

        public static Bitmap GetScreen()
        {
            var screenSize = SystemArguments.ScreenSize;
            var screenRect = new Rectangle(0, 0, screenSize.Width, screenSize.Height);
            var screenBmp = new Bitmap(screenRect.Width, screenRect.Height);
            var screenGraphics = Graphics.FromImage(screenBmp);
            //分屏：以主屏幕左上角为原点，左边副屏幕为负值
            screenGraphics.CopyFromScreen(0, 0, 0, 0, screenRect.Size);
            screenGraphics.DrawImage(screenBmp, 0, 0, screenRect, GraphicsUnit.Pixel);
            return screenBmp;
        }
    }
}
